package com.ptp.backendcertms.basiccompnents.data.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.ReportPo;
import com.ptp.backendcertms.basiccompnents.data.dao.mapper.IReportMapper;
import com.ptp.backendcertms.basiccompnents.data.dao.service.IReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.ptp.backendcertms.api.internal.constant.DateTypeConstant.DATE_TIME_PATTERN_SHORT_MATTER;

/**
 * 报告表 服务实现类
 */
@Service
public class ReportServiceImpl extends ServiceImpl<IReportMapper, ReportPo> implements IReportService {

    @Override
    public IPage<ReportPo> pageList(ReportPo queryPo, Integer current, Integer pageSize) {
        LambdaQueryWrapper<ReportPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportPo::getDelFlag, true);
        if(StringUtils.isNotBlank(queryPo.getOrganizationName())){
            queryWrapper.like(ReportPo::getOrganizationName, queryPo.getOrganizationName());
        }
        if(StringUtils.isNotBlank(queryPo.getReportNo())){
            queryWrapper.eq(ReportPo::getReportNo, queryPo.getReportNo());
        }
        queryWrapper.select(ReportPo::getReportNo, ReportPo::getOrganizationName,
                ReportPo::getReportName, ReportPo::getReleaseTime, ReportPo::getSerialNo);
        queryWrapper.orderByDesc(ReportPo::getId);
        IPage<ReportPo> page = new Page<>(current, pageSize);
        return page(page, queryWrapper);
    }

    @Override
    public void saveOrUpdateReportInfo(ReportPo entity) {
        if (Objects.isNull(entity.getId())) {
            entity.setCreateTime(LocalDateTime.now().format(DATE_TIME_PATTERN_SHORT_MATTER));
            save(entity);
        }
        updateById(entity);
    }

    @Override
    public IPage<ReportPo> pageListMc(ReportPo queryPo, String releaseStartTime, String releaseEndTime, Integer current, Integer pageSize) {
        LambdaQueryWrapper<ReportPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportPo::getDelFlag, true);
        if(StringUtils.isNotBlank(queryPo.getOrganizationName())){
            queryWrapper.like(ReportPo::getOrganizationName, queryPo.getOrganizationName());
        }
        if (StringUtils.isNotBlank(queryPo.getSocialCreditCode())) {
            queryWrapper.eq(ReportPo::getSocialCreditCode, queryPo.getSocialCreditCode());
        }
        if(StringUtils.isNotBlank(queryPo.getReportNo())){
            queryWrapper.eq(ReportPo::getReportNo, queryPo.getReportNo());
        }
        if(StringUtils.isNotBlank(queryPo.getReportName())){
            queryWrapper.eq(ReportPo::getReportName, queryPo.getReportName());
        }
        if (StringUtils.isNotBlank(releaseStartTime) && StringUtils.isNotBlank(releaseEndTime)) {
            queryWrapper.ge(ReportPo::getReleaseTime, releaseStartTime);
            queryWrapper.le(ReportPo::getReleaseTime, releaseEndTime);
        }
        queryWrapper.select(ReportPo::getSerialNo, ReportPo::getReportNo,
                ReportPo::getReportName, ReportPo::getSocialCreditCode,
                ReportPo::getOrganizationName, ReportPo::getReleaseTime);
        queryWrapper.orderByDesc(ReportPo::getId);
        IPage<ReportPo> page = new Page<>(current, pageSize);
        return page(page, queryWrapper);
    }

    @Override
    public ReportPo getBySerialNo(String serialNo) {
        LambdaQueryWrapper<ReportPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportPo::getDelFlag, true);
        queryWrapper.eq(ReportPo::getSerialNo, serialNo);
        queryWrapper.select(ReportPo::getSerialNo, ReportPo::getReportNo,
                ReportPo::getReportName, ReportPo::getSocialCreditCode,
                ReportPo::getOrganizationName, ReportPo::getReleaseTime);
        return getOne(queryWrapper);
    }

    @Override
    public void deleteBySerialNo(ReportPo updatePo) {
        LambdaUpdateWrapper<ReportPo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(ReportPo::getDelFlag, false);
        updateWrapper.eq(ReportPo::getSerialNo, updatePo.getSerialNo());
        baseMapper.update(updatePo,updateWrapper);
    }

    @Override
    public void updateReportBySerialNo(ReportPo reportPo) {
        LambdaUpdateWrapper<ReportPo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ReportPo::getSerialNo, reportPo.getSerialNo());
        baseMapper.update(reportPo,updateWrapper);
    }

}

